package com.pxl.springbootinit.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pxl.springbootinit.common.ErrorCode;
import com.pxl.springbootinit.exception.BusinessException;
import com.pxl.springbootinit.model.dto.task.TaskReviewForm;
import com.pxl.springbootinit.model.entity.Task;
import com.pxl.springbootinit.model.entity.TaskClaim;
import com.pxl.springbootinit.model.entity.TaskReview;
import com.pxl.springbootinit.model.entity.User;
import com.pxl.springbootinit.model.vo.TaskReviewVO;
import com.pxl.springbootinit.service.TaskClaimService;
import com.pxl.springbootinit.service.TaskReviewService;
import com.pxl.springbootinit.mapper.TaskReviewMapper;
import com.pxl.springbootinit.service.TaskService;
import com.pxl.springbootinit.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author MADAO
* @description 针对表【task_review(任务评价表)】的数据库操作Service实现
* @createDate 2024-12-20 11:59:03
*/
@Service
public class TaskReviewServiceImpl extends ServiceImpl<TaskReviewMapper, TaskReview>
    implements TaskReviewService{

    @Resource
    private UserService userService;
    @Resource
    private TaskService taskService;
    @Resource
    private TaskClaimService taskClaimService;

    /**
     * 新增任务评论
     * @param taskReviewForm
     * @return
     */
    @Override
    public Boolean addReview(TaskReviewForm taskReviewForm) {
        Long taskId = taskReviewForm.getTaskId();
        Task task = taskService.getById(taskId);
        if (task == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"任务不存在");
        }
        TaskReview taskReview = new TaskReview();
        BeanUtils.copyProperties(taskReviewForm,taskReview);
        // 当前用户评价
        taskReview.setReviewerId(task.getUserId());
        List<TaskClaim> taskClaimList = taskClaimService.list(new QueryWrapper<TaskClaim>().eq("task_id", taskId));
        if (CollectionUtil.isEmpty(taskClaimList)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"任务不存在");
        }
        TaskClaim taskClaim = taskClaimList.get(0);
        //被评价的用户，领取任务的人
        taskReview.setRevieweeId(taskClaim.getUserId());
        boolean result = this.save(taskReview);
        if (!result){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"评价失败，请重试");
        }

        return true;
    }

    @Override
    public Page<TaskReviewVO> pageReview(TaskReviewForm taskReviewForm) {
        int current = taskReviewForm.getCurrent();
        int pageSize = taskReviewForm.getPageSize();
        int start = (current - 1) * pageSize;
        int end = start + pageSize;
        List<TaskReviewVO> taskReviewVOList = this.baseMapper.selectTaskReview(taskReviewForm, start, end);
        Long total = this.baseMapper.countTaskReview(taskReviewForm);
        Page<TaskReviewVO> taskReviewVOPage = new Page<>();
        taskReviewVOPage.setTotal(total);
        taskReviewVOPage.setRecords(taskReviewVOList);
        return taskReviewVOPage;
    }

    @Override
    public List<TaskReviewVO> myReview(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null){
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR,"请先登录");
        }
        Long userId = loginUser.getId();
        List<TaskReview> taskReviewList = this.list(new QueryWrapper<TaskReview>().eq("reviewee_id", userId));
        if (CollectionUtil.isEmpty(taskReviewList)){
            return new ArrayList<>();
        }
        List<Long> taskIdList = taskReviewList.stream().map(TaskReview::getTaskId).collect(Collectors.toList());
        List<Task> taskList = taskService.listByIds(taskIdList);
        if (CollectionUtil.isEmpty(taskList)){
            return new ArrayList<>();
        }
        Map<Long, Task> taskMap = taskList.stream().collect(Collectors.toMap(Task::getId, task -> task));
        List<TaskReviewVO> taskReviewVOList = taskReviewList.stream().map(taskReview -> {
            TaskReviewVO taskReviewVO = getTaskReviewVO(taskReview);
            Task task = taskMap.get(taskReview.getTaskId());
            if (task != null){
                taskReviewVO.setTask(taskService.getTaskVO(task));
            }
            return taskReviewVO;
        }).collect(Collectors.toList());
        return taskReviewVOList;
    }

    private TaskReviewVO getTaskReviewVO(TaskReview taskReview){
        TaskReviewVO taskReviewVO = new TaskReviewVO();
        BeanUtils.copyProperties(taskReview,taskReviewVO);
        return taskReviewVO;
    }
}




